/**
 */

(function($,window,undefined){
    'use strict';
    $.fn.alert = function(){
    	if(typeof arguments[0]=="string")
			return $.fn.alert.methods[arguments[0]].apply(this,Array.prototype.slice.call(arguments,1));
    };
    $.fn.alert.methods = {
    	options:function(){
    		return $.data(this[0],'alert');
    	},
    	destroy:function(){
			return this.each(function(){
				var jq = $(this);
				var timeout = $(this).alert("options").timeout;
				if(timeout){
					clearTimeout(timeout);
				}
				setTimeout(function(){
					var onDestory = $.data(jq[0],"alert").onDestory;
					if(typeof onDestory=="function")
						onDestory.call(jq,$.data(jq[0],"alert"));
					jq.remove();
				},500)
				if($.data(this,'alert').msgAlign=="center"){
					$(this).removeClass("anamatetoCenter").addClass("anamatetoHide");
				}else{
					$(this).find(".context").removeClass("anamate"+$.data(this,'alert').msgAlign+"Show").addClass("anamate"+$.data(this,'alert').msgAlign+"Hide");
				}
			});
    	},
    	btns:function(){
    		return this.find(".btns");
    	}
    }
	$.alert = function(){
    	var buts = [{
			text:"确定",
			addClass:"red-btn",
			onClick:function(){$.data(this[0],'alert').onClickBtn('ok');this.alert("destroy");}
		},{
			text:"取消",
			onClick:function(){$.data(this[0],'alert').onClickBtn('cancel');this.alert("destroy");}
		}];
    	if(typeof arguments[0].buttons == "boolean"){
    		if(arguments[0].buttons) arguments[0].buttons = buts;
    		else arguments[0].buttons = [];
    	} else if(typeof arguments[0].buttons == "number"){
    		arguments[0].buttons = [buts[arguments[0].buttons]];
    	}
		var opt = $.extend(true,{},{closeable:true,model:true,onClickBtn:function(){}, buttons:[], msg:'',msgAlign:"center", title:"系统提示", onCreate:function(){ },onDestory:function(){}},arguments[0]||{});
		var body = $('body');
		var mask = $("<div class='mask'></div>").appendTo(body);
		$.data(mask[0],"alert",opt);
		
		var context = $("<div class='context'></div>").appendTo(mask);
		var head = $("<div class='header'></div>").appendTo(context);
		var content = $("<div class='content'></div>").appendTo(context);
		if(opt.noheader){
			head.hide();
		}
		if(opt.contextCss){
			context.css(opt.contextCss);
		}
		if(opt.closeable){
			$("<div class='closebtn'></div>").appendTo(context).click(function(){
				mask.alert("destroy");
			});
		}
		var btns = $("<div class='btns'></div>").appendTo(context);

		if(opt.nofooter){
			btns.hide();
		}
		$.each(opt.buttons,function(i,btn){
			var btn_a = $("<a title='"+btn.text+"' class='btn "+(function(cls){if(cls)return cls;else return '';})(btn.addClass)+"'>"+btn.text+"</a>");
			btn_a.on("click",function(){
				btn.onClick.call(mask);
				body.unbind("keydown");
			});
			//body.off();
			body.on("keydown",function(e){
				if(btn.keyBind == e.keyCode){
					btn_a.click();
					e.stopPropagation();
					return false;
				}
			})
			
			if(btn.css)
				btn_a.css(btn.css);
			btn_a.appendTo(btns);
		});
		content.html(opt.msg);
		context.css("left",($(window).width()-context.width())/2);
		$(window).on("resize",function(){
			context.css("left",($(window).width()-context.width())/2);
		});
		head.html(opt.title||"&nbsp;");
		
		mask.alert(opt);
		

		if(!opt.msgAlign){
			opt.msgAlign = "center";
		}
		if(opt.msgAlign=="center"){
			mask.addClass("anamatetoCenter");
		}else if(opt.msgAlign=="bottomright"){
			context.css({"right":"-100px","left":"auto","top":"auto","bottom":"10px"}).addClass("anamatebottomrightShow");
		}else if(opt.msgAlign=="bottomleft"){
			context.css({"right":"auto","left":"-100px","top":"auto","bottom":"10px"}).addClass("anamatebottomleftShow");
		}else if(opt.msgAlign=="topright"){
			context.css({"right":"-100px","left":"auto","top":"10px","bottom":"auto"}).addClass("anamatetoprightShow");
		}else if(opt.msgAlign=="topleft"){
			context.css({"right":"auto","left":"-100px","top":"10px","bottom":"auto"}).addClass("anamatetopleftShow");
		}
		
		if(opt.model==false){
			mask.css({"position":"initial","background":"none","z-index":"9999"});
			context.css({ "position":"fixed", "z-index":"99999999" });
		}
		else
			mask.css({"position":"fixed"});
		
		opt.onCreate.call(mask);
		return mask;
	}
})($,window)
define(function (require, exports, module) {
    'use strict';
	require("resource/css/alerts.css");
	
	/*$.alert({
		title:"xxx",
		msg:"xxx",
		msgAlign:"topright",
		buttons:false,
		model:false,
		onCreate:function(){
			var jq = this;
			this.find(".btns").text("2秒后自动关闭").css({'padding':'3px','text-align':'left'});
			setTimeout(function(){
				jq.alert("destroy");
			},10000)
		}
	});
	$.alert({
		msg:"单元与房屋生成成功,请进行房屋批量维护！",
		buttons:0,
		onClickBtn:function(){
		}
	});*/
	exports = {
    	alert:function(msg,upfunc,ext){
    		$.alert($.extend({
    			model:true,
    			buttons:[{
    				text:"确定",
    				keyBind:13,
    				addClass:"red-btn",
    				onClick:function(){
    					this.alert("destroy");
    					if(typeof upfunc=="function")
    						upfunc.call(this,true);
    				}
    			}],
    			msg:msg,
    			title:"系统提示"
    		},ext))
    	},
    	show:function(msg,upfunc){
    		$.alert({
    			msg:msg,
    			msgAlign:"bottomright",
    			title:"系统提示",
    			model:false,
    			onCreate:function(){
    				this.alert("btns").html("<span style='color:#333;font-size:12px;'>2秒后自动关闭！</span>").css({"text-align":"left",padding:"0 10px"});
    				var jq = this;
    				var timeout = setTimeout(function(){
    					jq.alert("destroy");
    					if(upfunc && typeof upfunc=="function"){
    						upfunc.call(this,true);
    					}

    				},2000);
    				this.alert("options").timeout = timeout;
    			}
    		});
    	},
    	confirm:function(msg,ok,cancel){
    		$.alert({
    			model:true,
    			buttons:[{
    				text:"确定",
    				addClass:"red-btn",
    				onClick:function(){$(this).alert("destroy");ok.call(this,true);}
    			},{
    				text:"取消",
    				onClick:function(){
    					$(this).alert("destroy");
    					if(typeof cancel=="function")
    						cancel();
    					else 
    						ok.call(this,false);
    				}
    			}],
    			msg:msg,
    			title:this.title
    		});
    	}
    }
    $.messager = {
		show:function(conf){
			var obj ;
			if(typeof conf=="string"){
				obj = {title:conf,msg:arguments[1],upfunc:arguments[2]};
			}else{
				obj = conf;
			}
			var title = obj.title,msg=obj.msg,upfunc = obj.upfunc;
			exports.show.call(obj,msg,upfunc);
		},
		alert: function(){
			var msg = arguments[1];
			var upfunc;
			if(arguments.length>3){
				upfunc=arguments[3];
			}
			exports.alert(msg,upfunc);
		},
		confirm:function(title,msg,upfunc){
			var obj ;
			if(typeof title=="string"){
				obj = {title:title,msg:msg,upfunc:upfunc};
			}else{
				obj = title;
				title = obj.title,msg=obj.msg,upfunc = obj.upfunc;
			}
			exports.confirm.call(obj,msg,upfunc);
		}
	}
	$.messager.defaults = {};
	return exports;
});